package com.example.shirotest.controller;

import com.example.shirotest.base.ResultVo;
import com.example.shirotest.entry.Glass;
import com.example.shirotest.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HomeController {

    //登录页面计算器
    private Integer count = 0;

    //应用启动时间
    private String startTime = DateUtil.getCurDateTimeStr();

    @RequestMapping("/home")
    public String home(){
        return "index";
    }

    @RequestMapping("/")
    public String login(Model model){
        model.addAttribute("count",++count);
        model.addAttribute("startTime",startTime);
        return "login.html";
    }

    @RequestMapping("/login")
    @ResponseBody
    public ResultVo login(String username, String password){
        Subject subject = SecurityUtils.getSubject();

        if(!subject.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);

            try{
                subject.login(token);
            }catch (AuthenticationException e){
                //表示用户的密码错误，这个异常是shiro在认证密码时抛出
                return  new ResultVo(0,e.getMessage(),null);
            }

        }

        return new  ResultVo(1,"登录成功","");
    }

    @RequestMapping("/success")
    public String success(){
        return "success.html";
    }

    @RequestMapping("/noPermission")
    public String noPermission(){
        return "noPermission.html";
    }


}
